package com.ddwl.user.controller;

import com.ddwl.common.annotation.Auth;
import com.ddwl.common.constant.BaseEnum;
import com.ddwl.common.controller.BaseController;
import com.ddwl.schema.bo.base.PageBo;
import com.ddwl.schema.bo.user.*;
import com.ddwl.schema.vo.base.PageVo;
import com.ddwl.schema.vo.base.RestVo;
import com.ddwl.schema.vo.order.StatisticsUserVo;
import com.ddwl.schema.vo.user.*;
import com.ddwl.user.service.UpgradeAgentOrderService;
import com.ddwl.user.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.math.BigDecimal;
import java.util.Collection;
import java.util.List;

/**
 * @author Fu Zhaohui
 * @date 2016/12/14
 */
@Api(tags = "会员服务")
@Auth
@RestController
public class UserController extends BaseController {

    @Autowired
    private UserService userService;
    @Resource
    private UpgradeAgentOrderService upgradeAgentOrderService;


    @GetMapping("/profile")
    @ApiOperation("获取会员信息")
    @Auth(product = {BaseEnum.ProductEnum.MALL, BaseEnum.ProductEnum.FENXIAO, BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.BYTE_DANCE, BaseEnum.ProductEnum.IOT})
    public RestVo<UserVo> getProfile() {
        return RestVo.ok(userService.getProfileById(getUserId(), getProduct()));
    }

    @PostMapping("/profile/update")
    @ApiOperation("更新会员信息")
    @Auth(product = {BaseEnum.ProductEnum.MALL, BaseEnum.ProductEnum.FENXIAO, BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.BYTE_DANCE, BaseEnum.ProductEnum.IOT})
    public RestVo updateProfile(@Valid @RequestBody EditUserInfoBo bo) {
        bo.setId(getUserId());
        userService.update(bo);
        return RestVo.ok();
    }

    @PostMapping("/profile/create")
    @ApiOperation("店铺生成分销会员信息")
    @Auth(product = {BaseEnum.ProductEnum.API})
    public RestVo createProfile(@Valid @RequestBody FenxiaoRegisterBo bo) {
        userService.fenxiaoRegister(bo);
        return RestVo.ok();
    }

    @PostMapping("/mobile/bind")
    @ApiOperation("绑定手机号码")
    @Auth(product = {BaseEnum.ProductEnum.MALL, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.BYTE_DANCE})
    public RestVo bindMobile(@RequestBody AuthBo bo) {
        bo.setUserId(getUserId());
        bo.setProduct(getProduct());
        userService.bindMobile(bo);
        return RestVo.ok();
    }

    @PostMapping("/openId/bind")
    @ApiOperation("绑定微信号")
    @Auth(product = {BaseEnum.ProductEnum.FENXIAO})
    public RestVo bindWX(@RequestBody WxUserBo bo) {
        bo.setUserId(getUserId());
        bo.setProduct(getProduct());
        userService.bindWxOpenUser(bo);
        return RestVo.ok();
    }

    @Deprecated
    @PostMapping("/openId/unbind")
    @ApiOperation("解绑微信号")
    @Auth(product = {BaseEnum.ProductEnum.FENXIAO})
    public RestVo unbindWX() {
//        WxOpenUserInfo bo=new WxOpenUserInfo();
//        bo.setUserId(getUserId());
//        bo.setProduct(getProduct());
//        userService.unbindWxOpenUser(bo);
        return RestVo.ok();
    }

    @PostMapping("/pwd/change")
    @ApiOperation("会员修改密码")
    @Auth(product = {BaseEnum.ProductEnum.MALL, BaseEnum.ProductEnum.FENXIAO, BaseEnum.ProductEnum.IOT})
    public RestVo changePwd(@RequestParam String oldPwd, @RequestParam String newPwd) {
        userService.changePwd(getUserId(), oldPwd, newPwd);
        return RestVo.ok();
    }

    @PostMapping("/pwd/reset")
    @Auth(required = false)
    @ApiOperation("会员忘记密码")
    public RestVo resetPwd(@Valid @RequestBody RegBo bo) {
        bo.setProduct(getAuthMap().get("pk"));
        userService.resetPwd(bo);
        return RestVo.ok();
    }

    @PostMapping("/page")
    @Auth(product = BaseEnum.ProductEnum.CONSOLE)
    @ApiOperation("分页查询用户")
    public RestVo<PageVo<UserVo>> pageUser(@Valid @RequestBody PageBo<UserSearchBo> pageBo) {
        return RestVo.ok(userService.page(pageBo));
    }

    @PostMapping("/pageUserCount")
    @Auth(product = BaseEnum.ProductEnum.API)
    @ApiOperation("分页查询用户数量")
    public RestVo<List<StatisticsUserVo>> pageUserCount(@RequestBody UserSearchBo pageBo) {
        return RestVo.ok(userService.pageUserCount(pageBo));
    }

    @GetMapping("/getUserAuth/{userId}")
    @ApiOperation(value = "获取会员微信绑定信息", hidden = true)
    @Auth(product = BaseEnum.ProductEnum.API)
    public RestVo<UserAuthVo> getUserAuth(@PathVariable("userId") String userId, @RequestParam String product) {
        return RestVo.ok(userService.getUserAuth(userId, product));
    }

    @PostMapping("/getUserAuthByOpenId")
    @ApiOperation(value = "openId获取会员微信绑定信息", hidden = true)
    @Auth(product = BaseEnum.ProductEnum.API)
    public RestVo<UserAuthVo> getUserAuthByOpenId(@RequestBody RegisterBo bo) {
        return RestVo.ok(userService.getUserAuthByOpenId(bo));
    }

    @PostMapping("/inviteCode/update")
    @ApiOperation("会员修改邀请码")
    @Auth(product = {BaseEnum.ProductEnum.CONSOLE, BaseEnum.ProductEnum.FENXIAO})
    public RestVo updateInviteCode(@Valid @RequestBody UpdateInviteCodeBo bo) {
        if(!StringUtils.hasText(bo.getUserId())) {
            bo.setUserId(getUserId());
        }
        userService.updateInviteCode(bo);
        return RestVo.ok();
    }

    @PostMapping("/wxCode/create")
    @ApiOperation("生成小程序码")
    //@Auth(product = BaseEnum.ProductEnum.CONSOLE)
    public RestVo createWxCode() {
        userService.createWxCode();
        return RestVo.ok();
    }

    @PostMapping("/setData")
    @ApiOperation("设置用户数据")
    @Auth(product = {BaseEnum.ProductEnum.FENXIAO, BaseEnum.ProductEnum.MALL})
    public RestVo<String> saveUserData(@RequestParam("content") String content) {
        return RestVo.ok(userService.saveUserData(getUserId(), content));
    }

    @PostMapping("/getData")
    @ApiOperation("获取用户数据")
    @Auth(required = false)
    public RestVo<String> getUserData(@RequestParam("userId") String userId) {
        return RestVo.ok(userService.getUserData(userId));
    }

    /**
     * 发放津贴
     *
     * @param
     * @return
     */
    @PostMapping("/provideSubsidy")
    @ApiOperation("后台给用户发放津贴")
    @Auth(product = {BaseEnum.ProductEnum.CONSOLE})
    public RestVo provideSubsidy(@RequestBody @Valid UserSubsidyBo bo) {
        userService.provideSubsidy(bo);
        return RestVo.ok();
    }

    /**
     * 针对砍价活动的新用户 （3天内第一次登录）
     */
    @GetMapping("/firstLogin")
    @Auth(product = BaseEnum.ProductEnum.API)
    RestVo<Boolean> firstLogin(@RequestParam String userId) {
        return RestVo.ok(userService.isNewUser(userId));
    }

    @GetMapping("/getInviteSubsidy")
    @Auth(product = {BaseEnum.ProductEnum.FENXIAO, BaseEnum.ProductEnum.MALL})
    @ApiOperation("获取邀请新人赠送津贴数量")
    public RestVo<BigDecimal> getInviteSubsidy() {
        return RestVo.ok(userService.getInviteSubsidy());
    }

    @PostMapping("/getUserByIds")
    @Auth(product = {BaseEnum.ProductEnum.API})
    @ApiOperation("根据用户ids获取用户列表")
    public RestVo<List<UserVo>> getUserByIds(@RequestParam Collection<String> userIds) {
        return RestVo.ok(userService.getByIds(userIds));
    }

    /**
     * 检查该微信用户是否已经注册过分销用户
     *
     * @return
     */
    @PostMapping("/checkRegister")
    @Auth(product = {BaseEnum.ProductEnum.API})
    @ApiOperation("检查该微信用户是否已经注册过分销用户")
    RestVo<LoginVo> checkRegister(@RequestBody RegisterBo bo) {
        return RestVo.ok(userService.checkRegister(bo));
    }

    /**
     * 保存店铺小程序码
     *
     * @param bo
     * @return
     */
    @PostMapping("/saveShopWxaCode")
    @Auth(product = {BaseEnum.ProductEnum.API})
    @ApiOperation("保存店铺小程序码")
    RestVo saveShopWxaCode(@RequestBody UserBo bo) {
        userService.saveShopWxaCode(bo);
        return RestVo.ok();
    }

    @PostMapping("/wbm/updateWxUserInfo")
    @Auth(product = {BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.BYTE_DANCE})
    @ApiOperation("修改微信信息")
    public RestVo updateWxUserInfo(@Valid @RequestBody WxUserBo bo) {
        bo.setUserId(getUserId());
        bo.setProduct(getProduct());
        userService.updateWxUserInfo(bo);
        return RestVo.ok();
    }

    @GetMapping("/getByOpenId/{openId}/{pk}")
    @Auth(product = {BaseEnum.ProductEnum.API})
    @ApiOperation("根据openId获取用户信息")
    public RestVo<UserVo> getByOpenId(@PathVariable("openId") String openId, @PathVariable("pk") String pk) {
        UserVo userVo = userService.getByOpenId(openId, pk);
        return RestVo.ok(userVo);
    }

    /**
     * 获取微信公众号openId
     *
     * @param userId
     * @return
     */
    @GetMapping("/getWechatOpenId")
    @ApiOperation("获取微信公众号openId")
    @Auth(product = BaseEnum.ProductEnum.API)
    public RestVo<String> getWechatOpenId(@RequestParam String userId) {
        return RestVo.ok(userService.getWechatOpenId(userId));
    }

    /**
     * 获取用户信息
     *
     * @param userId
     * @return
     */
    @GetMapping("/getUserById")
    @ApiOperation("获取简单用户信息")
    @Auth(product = BaseEnum.ProductEnum.API)
    RestVo<UserVo> getUserById(@RequestParam String userId) {
        return RestVo.ok(userService.getSimpleUserById(userId));
    }

    /**
     * 获取公益基金用户
     *
     * @return
     */
    @GetMapping("/getCharityFundUser")
    @ApiOperation("获取公益基金用户")
    @Auth(product = BaseEnum.ProductEnum.API)
    public RestVo<UserVo> getCharityFundUser() {
        return RestVo.ok(userService.getCharityFundUser());
    }

    @PostMapping("/changeAgent")
    @ApiOperation("更新代理或上下级关系")
    @Auth(product = {BaseEnum.ProductEnum.API, BaseEnum.ProductEnum.CONSOLE})
    public RestVo<String> changeAgent(@RequestBody UserAgentBo bo) {
        userService.changeAgent(bo);
        return RestVo.ok();
    }

    @PostMapping("/agent/become")
    @ApiOperation("普通用户交费成为代理")
    @Auth(product = {BaseEnum.ProductEnum.FENXIAO, BaseEnum.ProductEnum.WBM})
    public RestVo<UpgradeAgentPayOrderVo> become(@Valid @RequestBody AgentOrderBo bo){
        bo.setUserId(getUserId());
        bo.setUserName(getUserNameNew());
        bo.setPk(getProduct());
        return RestVo.ok(upgradeAgentOrderService.ordering(bo));
    }

    @PostMapping("/agent/pay")
    @ApiOperation("支付代理费")
    @Auth(product = {BaseEnum.ProductEnum.FENXIAO, BaseEnum.ProductEnum.WBM})
    public RestVo<String> pay(@Valid @RequestBody AgentPayOrderBo bo){
        fillBaseField(bo);
        return RestVo.ok(upgradeAgentOrderService.payAgentCost(bo));
    }

    @PostMapping("/agent/page")
    @ApiOperation("获取代理订单列表")
    @Auth(product = {BaseEnum.ProductEnum.CONSOLE})
    public RestVo<PageVo<UpgradeAgentOrderVo>> page(@Valid @RequestBody PageBo<UpgradeAgentPageBo> pageBo){
        return RestVo.ok(upgradeAgentOrderService.pageAgentOrder(pageBo));
    }

    @PostMapping("/agent/send")
    @ApiOperation("给代理发货")
    @Auth(product = {BaseEnum.ProductEnum.CONSOLE})
    public RestVo send(@Valid @RequestBody AgentSendBo bo){
        upgradeAgentOrderService.agentSend(bo);
        return RestVo.ok();
    }

    /**
     * 根据手机号或者邀请码获取用户
     *
     * @return
     */
    @GetMapping("/getSuperiorAgent")
    @ApiOperation("根据手机号获取用户")
    @Auth(product = {BaseEnum.ProductEnum.FENXIAO,BaseEnum.ProductEnum.WBM})
    public RestVo<UpgradeAgentVo> getSuperiorAgent(@RequestParam String keyWord) {
        return RestVo.ok(upgradeAgentOrderService.getSuperiorAgent(keyWord,getUserId()));
    }

}
